package services

import (
	"gitee.com/captials-team/ubdframe/src/common/utils"
	"gitee.com/captials-team/ubdframe/src/domain/converts"
	"gitee.com/captials-team/ubdframe/src/domain/dto"
	"gitee.com/captials-team/ubdframe/src/domain/dto/respdata"
	"gitee.com/captials-team/ubdframe/src/domain/interfaces"
	"gitee.com/captials-team/ubdframe/src/domain/models"
	v1log "gitee.com/captials-team/ubdframe/src/pkg/logs"
	"gitee.com/captials-team/ubdframe/src/pkg/passwd"
	"go.uber.org/dig"
)

type UserService struct {
	l       v1log.ILog
	userDao interfaces.ItfUser
}

func NewUserService(di *dig.Container, l v1log.ILog, userDao interfaces.ItfUser) *UserService {
	ctr := &UserService{
		l:       l,
		userDao: userDao,
	}

	return ctr
}

func (s *UserService) AutoCreateUser(nickname string, account string, password string) error {
	salt := utils.RandLetterFigureCode(16)
	_, err := s.userDao.AddCheckAccount(&models.User{
		Id:       0,
		Account:  account,
		Password: passwd.GenSaltPasswordByMd5(password, salt),
		Salt:     salt,
		Nickname: nickname,
		Avatar:   "",
		Email:    "",
		Status:   1,
	})
	return err
}

func (s *UserService) SaveUser(id int64, up *models.User) (int64, error) {
	if id > 0 {
		return s.updateUser(id, up)
	}
	return s.createUser(up)
}

func (s *UserService) createUser(user *models.User) (int64, error) {
	if user.Nickname == "" || user.Account == "" {
		return 0, respdata.CParamsInvalid
	}
	if user.Salt == "" {
		user.Salt = utils.RandLetterFigureCode(12)
	}
	if user.Password != "" {
		user.Password = passwd.GenSaltPasswordByMd5(user.Password, user.Salt)
	}

	find, err := s.userDao.QueryByAccount(user.Account)
	if err != nil {
		s.l.Error("query err %s", err)
		return 0, respdata.CSysBusy
	}
	if find != nil {
		return 0, respdata.CError.MMsg("用户名已存在")
	}
	_, err = s.userDao.Add(user)
	if err != nil {
		s.l.Error("add user err %s", err)
		return 0, respdata.CSysBusy
	}

	return user.Id, nil
}

func (s *UserService) updateUser(id int64, user *models.User) (int64, error) {
	if user.Nickname == "" || user.Account == "" {
		return 0, respdata.CParamsInvalid
	}

	find, err := s.userDao.QueryByAccount(user.Account)
	if err != nil {
		s.l.Error("query err %s", err)
		return 0, respdata.CSysBusy
	}
	if find != nil {
		return 0, respdata.CError.MMsg("用户名已存在")
	}
	_, err = s.userDao.Update(id, user)
	if err != nil {
		s.l.Error("add user err %s", err)
		return 0, respdata.CSysBusy
	}
	if user.Password != "" {
		_, err = s.userDao.ResetPassword(id, passwd.GenSaltPasswordByMd5(user.Password, find.Salt))
	}

	return user.Id, nil
}

func (s *UserService) DoAuth(param *dto.AccountLoginParams) (*dto.AuthInfo, error) {
	if param.Account == "" || param.Password == "" {
		return nil, respdata.CParamsInvalid
	}
	//ctr.l.Debug("auth req %v", req)

	user, err := s.userDao.QueryByAccount(param.Account)
	if err != nil {
		return nil, err
	}
	if user == nil || user.Password != passwd.GenSaltPasswordByMd5(param.Password, user.Salt) {
		return nil, respdata.CError.MMsg("account/password wrong")
	}

	//登录记录
	auth := converts.UserConvertAuthInfo(user)

	return &auth, nil
}

func (s *UserService) SignUp(param *dto.UserRegisterParams) (int64, error) {
	if param.Account == "" || param.Password == "" {
		return 0, respdata.CParamsInvalid
	}

	salt := utils.RandLetterFigureCode(12)
	user := &models.User{
		Account:   param.Account,
		Password:  passwd.GenSaltPasswordByMd5(param.Password, salt),
		Salt:      salt,
		Nickname:  param.Nickname,
		Email:     param.Email,
		Phone:     param.Phone,
		LastLogin: nil,
		LastIp:    "",
		Status:    1,
	}

	//检查账户是否存在
	if find, _ := s.userDao.QueryByAccount(user.Account); find != nil {
		return 0, respdata.CError.MMsg("account_exists")
	}

	id, err := s.userDao.Add(user)
	if err != nil {
		s.l.Ctl(err != nil).Error("add new user err %s", err)
		return 0, respdata.CTryAgain
	}
	return id, nil
}
